package com.sleepsocial.security;

//TODO a better encryption system
public class Encryptor {
    // The key for 'encrypting' and
    private static final String key = "SMYvGazrZhv24bLRyB4Ej832B8rCPLBkZEf2ZpqYvKS4ktfWUBKNzTU7adXZeWjFHGZYySwYCmxvL5XNvSpka44kqFBHJumtEPRTx8N3tF69cJ987cLvasNr";

    public static String encrypt(String str) {
        StringBuffer sb = new StringBuffer(str);

        int lenStr = str.length();
        int lenKey = key.length();

        //
        // For each character in our string, encrypt it...
        for (int i = 0, j = 0; i < lenStr; i++, j++) {
            if (j >= lenKey)
                j = 0; // Wrap 'round to beginning of key string.

            //
            // XOR the chars together. Must cast back to char to avoid compile
            // error.
            //
            sb.setCharAt(i, (char) (str.charAt(i) ^ key.charAt(j)));
        }

        return sb.toString();
    }

    public static String decrypt(String str) {
        // The same method, but the other way around
        return encrypt(str);
    }
}
